package com.changfang.gold_man.controller;

import com.changfang.gold_man.common.ImageUtil;
import com.changfang.gold_man.common.ResCode;
import com.changfang.gold_man.common.ResultVO;
import com.changfang.gold_man.mapper.UserMapper;
import com.changfang.gold_man.service.Imp.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController

@RequestMapping("/user")
@CrossOrigin(value = "*", maxAge = 1800, allowedHeaders = "*")

public class UserController {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserServiceImpl userServiceImpl;

    //注册用户
    @PostMapping("/register")
    public ResultVO register(@RequestBody Map<String, String> user) {
        String flag = userServiceImpl.register(user);
        if (flag.equals("1")) {
            return new ResultVO(ResCode.SUCCESS, "注册成功");
        } else {
            return new ResultVO(ResCode.ERROR, flag);
        }
    }

    //用户登录
    @PostMapping("/login")
    public ResultVO login(@RequestBody Map<String, String> user) {
        try {
            return new ResultVO(ResCode.SUCCESS, userServiceImpl.login(user));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "密码或用户名错误");
        }
    }

    //重置密码
    @PostMapping("/resetPass")
    public ResultVO resetPass(@RequestBody Map<String, String> resetPass) {
        try {
            String user_id = resetPass.get("user_id");
            String new_pass = resetPass.get("new_pass");
            return new ResultVO(ResCode.SUCCESS, userServiceImpl.updatePass(user_id, new_pass));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "重置密码失败");
        }
    }

    //修改用户权限
    @GetMapping("/changePms")
    public ResultVO changePms(@RequestParam String user_id, @RequestParam String user_permission) {
        try {
            int pms = Integer.parseInt(user_permission);
            return new ResultVO(ResCode.SUCCESS, userServiceImpl.changePms(user_id, pms));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "修改用户权限失败");
        }

    }

    //    查询所有用户
    @GetMapping("/all")
    public ResultVO getUser() {
        try {
            return new ResultVO(ResCode.SUCCESS, userServiceImpl.findAll());
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "查询失败");
        }

    }

    //    根据id查询用户
    @GetMapping("/id")
    public ResultVO getId(@RequestParam String user_id) {
        try {
            return new ResultVO(ResCode.SUCCESS, userServiceImpl.findId(user_id));
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "查询失败");
        }
    }

    //    添加用户
    @PostMapping("/add")
    public ResultVO addUser(@RequestBody Map<String, String> user) {
        String flag = userServiceImpl.addUser(user);
        if (flag.equals("1")) {
            return new ResultVO(ResCode.SUCCESS, "添加用户成功");
        } else {
            return new ResultVO(ResCode.ERROR, flag);
        }
    }

    //    根据id删除用户
    @GetMapping("/delete")
    public ResultVO deleteUser(@RequestParam String user_id) {
        System.out.println(user_id);
        try {
            userServiceImpl.deleteUser(user_id);
            return new ResultVO(ResCode.SUCCESS, "删除成功");
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "删除失败");
        }
    }

    //批量删除用户
    @PostMapping("/deleteUsers")
    public ResultVO deleteUsers(@RequestBody String usersId) {
        try {
            String[] arr = usersId.split(",");
            for (int i = 0; i < arr.length; i++) {
                System.out.println(arr[i]);
                userServiceImpl.deleteUser(arr[i]);
            }
            return new ResultVO(ResCode.SUCCESS, "批量删除成功");
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "批量删除失败");
        }
    }

    // 修改用户信息
    @PostMapping("/update")
    public ResultVO updateUser(@RequestBody Map<String, String> user) {
        try {
            userServiceImpl.updateUser(user);
            return new ResultVO(ResCode.SUCCESS, "修改信息成功");
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "修改信息失败" + e);
        }
    }

    //更新金币
    @GetMapping("/updateGold")
    public ResultVO updateGold(@RequestParam String user_id, @RequestParam String gold) {
        try {
            userServiceImpl.updateGold(user_id, gold);
            return new ResultVO(ResCode.SUCCESS, "修改金币成功");
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "修改金币失败");
        }
    }

    //更新积分
    @GetMapping("/updateScore")
    public ResultVO updateScore(@RequestParam String user_id, @RequestParam String score) {
        try {
            userServiceImpl.updateScore(user_id, score);
            return new ResultVO(ResCode.SUCCESS, "修改积分成功");
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "修改积分失败");
        }
    }

    //购买商品
    @PostMapping("/buyGoods")
    public ResultVO buyGoods(@RequestBody Map<String, String> buyInfo) {
        try {
            userServiceImpl.buyGoods(buyInfo);
            return new ResultVO(ResCode.SUCCESS, "购买成功");
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "购买失败");
        }
    }

    //租用车辆
    @PostMapping("/rentCar")
    public ResultVO rentCar(@RequestBody Map<String, String> carInfo) {
        try {
            userServiceImpl.rentCar(carInfo);
            return new ResultVO(ResCode.SUCCESS, "租用成功");
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "租用失败");
        }
    }

    //上传图片
    @PostMapping("/uploadImg")
    public ResultVO upload(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException {
//        如果文件不为空
        if (!file.isEmpty()) {
            String user_id = request.getParameter("userId");
            String imgType = request.getParameter("imgType");
            if (imgType.equals("head")) {
                String headUrl = ImageUtil.fileUpload(file);
                userMapper.updateImg(user_id, headUrl);
                HashMap<String, String> map = new HashMap<>();
                map.put("user_id", user_id);
                map.put("headUrl", headUrl);
                return new ResultVO(ResCode.SUCCESS, map);
            } else if (imgType.equals("bg")) {
                String bgUrl = ImageUtil.fileUpload(file);
                userMapper.updateBg(user_id, bgUrl);
                HashMap<String, String> map = new HashMap<>();
                map.put("user_id", user_id);
                map.put("bgUrl", bgUrl);
                return new ResultVO(ResCode.SUCCESS, map);
            }
        }
        return new ResultVO(ResCode.ERROR, "上传失败");
    }

    //    获取注册验证码
    //注册1336598重置1371851
    @GetMapping("/getCode")
    public ResultVO getCode(@RequestParam String user_id) {
        try {
            String code = userServiceImpl.getCode(user_id, "1336598");
            return new ResultVO(ResCode.SUCCESS, code);
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "获取验证码失败");
        }
    }

    //    获取重置密码验证码
    @GetMapping("/getResetCode")
    public ResultVO getResetCode(@RequestParam String user_id) {
        try {
            String code = userServiceImpl.getCode(user_id, "1371851");
            return new ResultVO(ResCode.SUCCESS, code);
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "获取验证码失败");
        }
    }

    //获取校园密语
    @GetMapping("/getSecret")
    public ResultVO getSecret() {
        return new ResultVO(ResCode.SUCCESS, userServiceImpl.getSecret());
    }

    //添加校园密语
    @PostMapping("/addSecret")
    public ResultVO addSecret(@RequestBody Map<String, String> secret) {
        String user_id = secret.get("user_id");
        String secret_msg = secret.get("secret_msg");
        if (userServiceImpl.addSecret(user_id, secret_msg).equals("success")) {
            return new ResultVO(ResCode.SUCCESS, "添加成功");
        } else {
            return new ResultVO(ResCode.ERROR, "添加失败");
        }

    }

    //点赞，取消赞
    @GetMapping("/like")
    public ResultVO like(@RequestParam String secret_id,@RequestParam Integer like) {
        try {
            userServiceImpl.like(secret_id,like);
            return new ResultVO(ResCode.SUCCESS, "点赞成功");
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "点赞失败");
        }
    }

    //删除密语
    @GetMapping("/deleteSecret")
    public ResultVO deleteSecret(@RequestParam String secret_id) {
        try {
            userServiceImpl.deleteSecret(secret_id);
            return new ResultVO(ResCode.SUCCESS, "删除密语成功");
        } catch (Exception e) {
            return new ResultVO(ResCode.ERROR, "删除密语失败");
        }
    }
}

